var app=getApp();
Page({
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
    wx.setNavigationBarTitle({
       title: '新闻汇总',
    })
  },
  data:{
     currentSelectedNavBar:"0",
     topTitleArray:['新闻资讯','NBA资讯','汽车资讯','娱乐播报','军事速递'],
     newsData:[],
     nbaNewsData:[],
     index:1,
     images:{},
  },
  // nba图片的接口中返回了图片的宽高，将宽高比算出来把算到的实际高度存储起来
  imageLoad: function(e) {
     var $width=e.detail.width,    //获取图片真实宽度
         $height=e.detail.height,
         ratio=$width/$height;    //图片的真实宽高比例
     var viewWidth=750,           //设置图片显示宽度
         viewHeight=750/ratio;    //计算的高度值
      var image=this.data.images; 
      //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
      image[e.target.dataset.index]={
         width:viewWidth,
         height:viewHeight
      }
      this.setData({
           images:image
      })
  },
  // 顶部切换
  switchTab:function(e){
    console.log(e.target.dataset.idx)
    this.setData({
        currentSelectedNavBar:e.target.dataset.idx
    })
    this.data.currentSelectedNavBar==1?this.netForNba():""
  },
  onLoad:function(){
    this.netForNewsInfo()
  },
  // 点击了cell
  cellClick:function(e){
    var item = this.data.newsData[e.currentTarget.dataset.index];
    wx.navigateTo({
       url: '../Detail/Detail?id='+item.stitle
    })
  },
  // 新闻资讯
  netForNewsInfo:function(){
    var that = this;
    app.showHud()
    wx.request({
      url: 'http://lib.wap.zol.com.cn/ipj/docList/?v=4.0&class_id=0&isReviewing=NO&last_time=2015-12-07%2010%3A14&',
      data: {
        page:that.data.index,
        retina:'1',
        vs:'iph440',
      },
      header: {
      },
      success: function(res) {
        console.log("news=====success")
        console.log(res.data.list)
        that.setData({
          newsData:res.data.list,
        })
      },
      fail: function (res) {
        console.log("failed")
      }
    })
  },
   // 新闻资讯
  upperNews: function () {
    var that = this;
    wx.showNavigationBarLoading()
    that.setData({
       index:1,
    }),
    this.netForNewsInfo();
    console.log("upper");
    setTimeout(function(){wx.hideNavigationBarLoading();wx.stopPullDownRefresh();}, 2000);
  },
  // 新闻资讯上拉加载
  lowerNews: function (e) {
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
       index:that.data.index+1,
    }),
    setTimeout(function(){wx.hideNavigationBarLoading();that.netRequestMore();}, 1000);
    console.log("lower")
  },
  netRequestMore:function(){
    var that = this;
    app.showHud()
    wx.request({
      url: 'http://lib.wap.zol.com.cn/ipj/docList/?v=4.0&class_id=0&isReviewing=NO&last_time=2015-12-07%2010%3A14&',
      data: {
        page:that.data.index,
        retina:'1',
        vs:'iph440',
      },
      header: {
      },
      success: function(res) {
        console.log(res.data.items)
        that.setData({
          newsData:that.data.newsData.concat(res.data.list),
        })
      },
      fail: function (res) {
        console.log("failed")
      }
    })
  },

  // NBA
  netForNba:function(){
    var that = this;
    app.showHud()
    wx.request({
      url: 'http://platform.sina.com.cn/sports_client/feed?ad=1&pos=nba&sport_tour=nba&__os__=iphone&partner=clear&',

      data: {
        pdps_params:'timestamp:+{{this.createTimeStamp()}}',
        feed_id:'653',
        __version__:'3.2.0',
        client_weibouid:'',
        f:'stitle,wapsummary,img,images,comment_show,comment_total,ctime,video_info',
        client_deviceid:'ea636e803fb7ca97ea250f9d6dee1648',
        app_key:'2923419926',
      },
      header: {
      },
      success: function(res) {
        console.log("NBA=====success")
        console.log(res.data.result.data.feed.data)
        that.setData({
          nbaNewsData:res.data.result.data.feed.data,
        })
      },
      fail: function (res) {
        console.log("failed")
      }
    })
  },
  // 获取时间戳
   createTimeStamp: function() {
   return parseInt(new Date().getTime() / 1000) + ''
   }
})